<template>
  <view class="container" :class="[device.lang]">
    <view class="store-list-top">
      <view class="store-header">
        <image :src="store" mode=""></image>
        <view class="">{{ t("Select the store") }}:</view>
      </view>
    </view>
    <view class="store-list" v-for="item in shopList" @click="goToView(item)"
      :style="{ backgroundColor: item.isSelected ? '#004B41' : (item.shopStatus === '1' ? '#AEAEAE' : '') }">
      <view class="store-list-shop">{{ item.shopName }}</view>
      <view class="icon-container">
        <image class="icon-image" :src="item.isSelected ? right1Solid : right1" mode=""></image>
      </view>
      <view v-if="item.shopStatus === '1'" class="mark">
        <div class="text">Closed</div>
      </view>
    </view>

  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import { useAuthStore, useDeviceStore } from "@/store";
import store from "@/static/images/login/switchStore.png";
import right1 from "@/static/images/login/left.png";
import right1Solid from "@/static/images/login/left.png";

const device = useDeviceStore()
const { t } = useI18n();
const deviceStore = useDeviceStore();
const authStore = useAuthStore();
const shopList = ref<Array<{ shopName: string, isSelected: boolean }>>([]);

onMounted(async () => {
  const result = await authStore.getMyShop();
  if (result.data.length === 1) {
    console.log(666, result);
    goToView(result.data[0]);
  }
  shopList.value = result.data.map(item => ({ ...item, isSelected: false }));
  console.log('=======>>>>', shopList.value);
});

const goToView = (data: { shopName: string }) => {
  shopList.value.forEach(item => item.isSelected = false);
  const selectedItem = shopList.value.find(item => item.shopName === data.shopName);
  if (selectedItem) selectedItem.isSelected = true;
  deviceStore.setShop(data);
  uni.switchTab({
    url: "/pages/index/index"
  });
};
</script>

<style scoped lang="scss">
@import "@/static/css/index.scss";

.store-list-top {
  height: 400rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 200rpx;

  .store-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 32rpx;
    font-family: PingFang SC, PingFang SC-Medium;
    font-weight: Medium;
    text-align: center;
    color: #898989;

    image {
      margin-top: 240rpx;
      width: 100rpx;
      height: 100rpx;
      margin-bottom: 30rpx;
    }
  }
}

.store-list {
  position: relative;
  width: 90%;
  margin: 0 auto;
  background: $primary-d;
  border-radius: 16rpx;
  height: 134rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30rpx;
  overflow: hidden;

  .store-list-shop {
    flex: 1;
    font-size: 30rpx;
    color: $primary-f;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .icon-container {
    width: 120rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;

    .icon-image {
      width: 40rpx;
      height: 40rpx;
    }
  }

  .mark {
    position: absolute;
    top: 0rpx;
    left: 0rpx;
    width: 0px;
    height: 0px;
    border-top: 104rpx solid #ED6347;
    border-right: 152rpx solid transparent;

    .text {
      color: #fff;
      white-space: nowrap;
      transform: rotate(-35deg);
      position: absolute;
      top: -85rpx;
      left: 5rpx;
      // color: white;
      font-weight: bold;
      text-align: center;
      font-size: 28rpx;
    }
  }
}

// :global(.ar) {
//   .mark {
//     left: auto;
//     right: 0rpx;
//     border-top: 104rpx solid #ED6347;
//     border-right: none;
//     border-left: 152rpx solid transparent;

//     .text {
//       transform: rotate(35deg);
//       left: auto;
//       right: 5rpx;
//     }
//   }
// }

.Arab .store-list {
  flex-flow: row-reverse;

  .mark {
    left: auto;
    right: 0rpx;
    border-top: 104rpx solid #ED6347;
    border-right: none;
    border-left: 152rpx solid transparent;

    .text {
      transform: rotate(35deg);
      left: auto;
      right: 5rpx;
    }
  }
}

.Arab .icon-image {
  transform: rotate(180deg);
}
</style>